package test;

/**
 * two head link list
 */
public class TwoHeadNodes {

    private int num = 0;

    private Node first;

    private Node last;

    public static void main(String[] args) {
        new TwoHeadNodes().go();
    }

    private void go() {
        for (int i = 0; i < 100; i++) {
            Node node = new Node();
            node.num = i;
            //addHead(node);

            addTail(node);
        }

        listAll();
        System.out.println("total num is:" + count());
        System.out.println("first is:" + first);
        System.out.println("last is:" + last);
    }

    int count() {
        return num;
    }

    void deleteTail() {

    }

    void deleteHead() {

    }

    void addHead(Node node) {
        num++;
        if (first == null) {
            first = node;
            last = node;
        } else {
            Node temp = first;
            first = node;

            first.next = temp;
            temp.prev = first;
        }
    }

    void addTail(Node node) {
        num++;
        if (last == null) {
            first = node;
            last = node;
        } else {
            Node temp = last;
            last = node;

            last.prev = temp;
            temp.next = last;
        }
    }

    void listAll() {
        Node temp = first;
        if (temp != null) {
            System.out.println(temp.num);
            while (temp.next != null) {
                System.out.println(temp.next.num);
                temp = temp.next;
            }
        }
    }

    static class Node {

        public int num;
        public Node next;
        public Node prev;

        @Override
        public String toString() {
            return "Node{" +
                    "num=" + num +
                    '}';
        }
    }


}
